package com.biz.primus.app.erpbridge.controller;

import com.biz.primus.app.erpbridge.feign.OmsFeignClient;
import com.biz.primus.app.erpbridge.service.OmsQueryCacheService;
import com.biz.primus.app.erpbridge.stream.OmsCollectionSource;
import com.biz.primus.app.erpbridge.trans.SaleOrderTransformer;
import com.biz.primus.app.erpbridge.vo.oms.OmsDepotOrderNum;
import com.biz.primus.app.erpbridge.vo.oms.OrderType;
import com.biz.primus.app.erpbridge.vo.oms.SaleOrder;
import com.biz.primus.app.erpbridge.vo.oms.SaleOrderHeader;
import com.biz.primus.app.erpbridge.vo.oms.request.*;
import com.biz.primus.base.support.rest.vo.JsonResult;
import com.biz.primus.model.oms.vo.OmsOrderReturnVo;
import com.biz.primus.model.oms.vo.OmsOrderVo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Info;
import io.swagger.annotations.SwaggerDefinition;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yanweijin
 * @date 2017/11/3
 */

@RestController
@RequestMapping("bridge/oms")
@SwaggerDefinition(info = @Info(title = "中台订单系统接口",version = "1.0.0",description = "ERP和POS查询/操作/推送中台订单"))
@Slf4j
public class OmsBridgeController extends BaseBridgeController {


	@Autowired
	private OmsFeignClient omsFeignClient;

	private SaleOrderTransformer saleOrderTransformer = SaleOrderTransformer.INSTANCE;

	@Autowired
	private OmsCollectionSource collectionSource;

	@Autowired
	private OmsQueryCacheService omsQueryCacheService;


	@PostMapping("orders")
	@ApiOperation(value="OMS收单",notes = "异步处理,返回code=0并不表示OMS能正确处理订单")
	public JsonResult pushOrder(@RequestBody SaleOrder order){
		log.info("bridge收单: 接收到POS订单,原始数据:{}",order);
		if (order.getType()== OrderType.orders) {
			OmsOrderVo payload = saleOrderTransformer.toOmsOrderVo(order);
			Message<OmsOrderVo> msg = MessageBuilder.withPayload(payload).build();
			collectionSource.orderDataOutput().send(msg);
		}else if(order.getType()==OrderType.returns){
			OmsOrderReturnVo payload = saleOrderTransformer.toOmsOrderReturnVo(order);
			Message<OmsOrderReturnVo> msg = MessageBuilder.withPayload(payload).build();
			collectionSource.orderReturnDataOutput().send(msg);
		}else {
			throw new IllegalArgumentException("type只支持'orders'或'returns'");
		}
		return new JsonResult();
	}

	//1 订单和退单列表查询
	@GetMapping("list")
	@ApiOperation(value="订单和退单列表查询", position = 1,responseContainer = "list",response = SaleOrderHeader.class)
	public JsonResult omsList(OmsListQueryVo req){
		List<SaleOrderHeader> resp = omsQueryCacheService.queryOrders(req);
		return new JsonResult(resp);
	}

	//2 订单和退单详情查询
	@GetMapping("detail")
	@ApiOperation(value="订单和退单详情查询", position = 2,response = SaleOrder.class)
	public JsonResult omsDetail(OmsDetailQueryVo req){
		SaleOrder resp = omsQueryCacheService.queryOrderDetail(req);
		return new JsonResult(resp);
	}

	//3 订单状态变更
	@PostMapping("state")
	@ApiOperation(value="订单状态变更", position = 3)
	public JsonResult stateChange(@RequestBody OrderStateChangeRequestOrder req){
		return new JsonResult();
	}

	//4 退货确认入库
	@PostMapping("inbound")
	@ApiOperation(value="退货确认入库", position = 4)
	public JsonResult returnInbound(@RequestBody DepotOrderOperationRequest req){


		return new JsonResult();
	}

	//5 货到付款现金转支付
	@PostMapping("recordcash")
	@ApiOperation(value="货到付款现金转支付", position = 5)
	public JsonResult recordCash(@RequestBody RecordCashRequest req){


		return new JsonResult();
	}

	//6 修改配送信息
	@PostMapping("delivery")
	@ApiOperation(value="修改配送信息", position = 6)
	public JsonResult modifyDelivery(@RequestBody ModifyDeliveryRequest req){


		return new JsonResult();
	}

	//7 申请改派
	@PostMapping("redispatching")
	@ApiOperation(value="门店申请改派", position = 7)
	public JsonResult redispatching(@RequestBody DepotOrderOperationRequest req){


		return new JsonResult();
	}

	//8 自提码查询订单
	@GetMapping("taketheirdetail")
	@ApiOperation(value="自提订单详情查询", position = 8, response = SaleOrder.class)
	public JsonResult takeTheirDetail(@RequestBody OmsTakeTheriDetailQuery req){

		SaleOrder resp = null;
		return new JsonResult(resp);
	}

	//9 签收并支付
	@PostMapping("receive")
	@ApiOperation(value="签收并支付", position = 9)
	public JsonResult receive(@RequestBody ReceiveAndPayRequest req){


		return new JsonResult();
	}

	//10 填写物流信息
	@PostMapping("fillexpress")
	@ApiOperation(value="填写物流信息", position = 10)
	public JsonResult fillExpress(@RequestBody FillExpressRequest req){


		return new JsonResult();
	}

	//12 门店订单数量提示
	@GetMapping("numtips")
	@ApiOperation(value="门店订单数量提示", position = 12, response = OmsDepotOrderNum.class)
	public JsonResult orderNumTips(BaseDepotRequest req){

		OmsDepotOrderNum resp = null;
		return new JsonResult(resp);
	}




}
